package com.anhuanjia.tools.eao.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.anhuanjia.tools.eao.bean.EaoPublicity;
import com.anhuanjia.tools.eao.bean.EaoPublicityAudit;
import com.anhuanjia.tools.eao.bean.EaoPublicityEnclosure;
import com.anhuanjia.tools.eao.bean.EaoPublicityVisit;
import com.anhuanjia.tools.eao.beanvo.EaoPublicityTypeVo;
import com.anhuanjia.tools.eao.beanvo.EaoPublicityVo;
import com.anhuanjia.tools.eao.util.BanWordsUtil;
import com.anhuanjia.tools.eao.util.FindDateUtil;
import com.anhuanjia.tools.eao.util.GetLatAndLng;
import com.efastsoft.framework.Member;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.UserInfo;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.utility.StringUtil;

/**
 * 公示表操作
 * 
 * @author Administrator
 *
 */

@Alias(value = "publicity")
public class PublicityUI extends UIFacade {
	/**
	 * 地图弹框下边显示4条点击量最高的公示
	 */
	@Priv(login = false)
	public void findfourPublicity() {
		DataTable dt = new Q(
				"select * from eao_publicity where State not in(0,1) and IsDelete = 0 order by VisitNumber desc limit 0,4 ").fetch();
		List<EaoPublicityVo> list = new ArrayList<EaoPublicityVo>();
		if (dt.getRowCount() > 0) {
			for (DataRow dataRow : dt) {
				EaoPublicityVo publicity = new EaoPublicityVo();
				publicity.setId(dataRow.getString("Id"));
				publicity.setTitle(dataRow.getString("Title"));
				publicity.setRealName(dataRow.getString("RealName"));
				publicity.setCreateTime(dataRow.getString("PublicityTime"));
				list.add(publicity);
			}
			this.Response.put("publicCityList", list);
		} else {
			$F("查询击量最高的4条公示失败");
		}
	}

	/**
	 * 查询一次、二次、验收、全本、其他公示 projectType 0,1,2,3,4,5一次、二次、验收、全本、其他公示
	 **/
	@Priv(login = false)
	public void findOneOrTwoPublicity() {
		Integer dtNum = 0;

		String ProjectType = $V("ProjectType");
		int pageSize = this.Request.getInt("PageSize");
		if (pageSize == 0) {
			pageSize = 10;
		}
		int pageIndex = this.Request.getInt("PageIndex");
		Q q = new Q("select * from eao_publicity where 1=1 ");
		Q q2 = new Q("select count(1) as num from eao_publicity where 1=1 ");

		if (!StringUtil.isEmpty(ProjectType)) {
			q.and().eq("ProjectType", ProjectType);
			q2.and().eq("ProjectType", ProjectType);
		}

		String State = $V("State");
		if (StringUtil.isEmpty(State)) {
			q.and().append(" State != '0'");
			q2.and().append(" State != '0' ");
		} else {
			q.and().in("State", State);
			q2.and().in("State", State);
		}
		boolean isShowDel = false;
		isShowDel = Boolean.valueOf($V("IsShowDel"));
		if (isShowDel) {
			$V("IsRelease");
			String memberId = String.valueOf(Member.getUID());
			if (!StringUtil.isEmpty(memberId)) {
				boolean isRelease = Boolean.valueOf($V("IsRelease"));
				if (isRelease) {
					q.and().eq("Operator", memberId);
					q2.and().eq("Operator", memberId);
					q.and().eq("IsDelete", "0");
					q2.and().eq("IsDelete", "0");
				} else {
					q.and().eq("Operator", memberId);
					q2.and().eq("Operator", memberId);
				}
			}

		} else {
			q.and().eq("IsDelete", "0");
			q2.and().eq("IsDelete", "0");

		}
		q.orderby("PublicityTime desc");
		DataTable dt = q.fetch(pageSize, pageIndex);
		DataTable dnum = q2.fetch();

		List<EaoPublicity> publicCityList = new ArrayList<EaoPublicity>();
		if (dt.getRowCount() > 0) {
			for (DataRow dataRow : dt) {
				EaoPublicity publicity = new EaoPublicity();
				publicity.setValue(dataRow);
				publicCityList.add(publicity);
			}
			this.Response.put("publicCityList", publicCityList);
		} else {
			$F("查询公示失败");
		}
		if (dnum.getRowCount() > 0) {
			for (DataRow dataRow : dnum) {
				dtNum = dataRow.getInt("num");
			}
			this.Response.put("publicCityListNum", dtNum);
		} else {
			$F("查询公示数量失败");
		}
	}

	/**
	 * 半年内所有公示
	 */
	@Priv(login = false)
	public void findPublicityWithTime() {
		// 模糊查询查询所有公示
		Q q = new Q(
				"select p.Id,p.Title,mh.RealName,p.PublicityTime from eao_publicity as p,Ec_Member as mh where p.IsDelete = 0 and p.State != 0 and mh.Id = p.Operator");
		// 查询半年内所有人发布成功的公示
		q.append(
				" and date_sub(curdate(), interval 6 month) <= date(p.PublicityTime) order by p.PublicityTime desc ");
		DataTable dt = q.fetch();
		List<EaoPublicityVo> publicCityList = new ArrayList<EaoPublicityVo>();
		if (dt.getRowCount() > 0) {
			for (DataRow dataRow : dt) {
				EaoPublicityVo publicity = new EaoPublicityVo();
				publicity.setId(dataRow.getString("Id"));
				publicity.setTitle(dataRow.getString("Title"));
				publicity.setRealName(dataRow.getString("RealName"));
				publicity.setCreateTime(dataRow.getString("PublicityTime"));
				publicCityList.add(publicity);
			}
			this.Response.put("allCityList", publicCityList);
		} else {
			$F("查询所有发布成功公示失败");
		}
	}

	/**
	 * 模糊查询所有公示、未审核公示
	 */
	@Priv(login = false)
	public void findLikePublicity() {
		// 模糊查询查询所有公示
		String title = $V("Title");
		int pageSize = this.Request.getInt("PageSize");
		if (pageSize == 0) {
			pageSize = 10;
		}
		int pageIndex = this.Request.getInt("PageIndex");
		Q q = new Q(
				"select p.Id,p.Title,mh.RealName,p.PublicityTime from eao_publicity as p,Ec_Member as mh where p.IsDelete = 0 and p.State = 2 and mh.Id = p.Operator");
		Q dtNum1 = new Q(
				"select count(1) as num from eao_publicity as p,Ec_Member as mh where p.IsDelete = 0 and p.State = 2 and mh.Id = p.Operator ");
		if (StringUtil.isNotEmpty(title)) {
			q.append(" and p.Title like ?", "%" + title + "%");
			dtNum1.append(" and p.Title like ?", "%" + title + "%");
		}
		q.append(" order by p.PublicityTime desc");
		dtNum1.append("  order by p.PublicityTime desc");

		DataTable dt = q.fetch(pageSize, pageIndex);
		List<EaoPublicityVo> publicCityList = new ArrayList<EaoPublicityVo>();
		if (dt.getRowCount() > 0) {
			for (DataRow dataRow : dt) {
				EaoPublicityVo publicity = new EaoPublicityVo();
				publicity.setId(dataRow.getString("Id"));
				publicity.setTitle(dataRow.getString("Title"));
				publicity.setRealName(dataRow.getString("RealName"));
				publicity.setCreateTime(dataRow.getString("PublicityTime"));
				publicCityList.add(publicity);
			}
			this.Response.put("publicCityList", publicCityList);

		} else {
			$F("模糊查询所有公示失败");
		}
		DataTable dtNum = dtNum1.fetch();

		Integer publiCityNum = 0;
		for (DataRow dataRow : dtNum) {
			publiCityNum = dataRow.getInt("num");
		}
		this.Response.put("countNum", publiCityNum);

	}
	
	/**
	 * 保存草稿
	 * 
	 * @throws Exception
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdatePublicity() throws Exception {
		String id = $V("Id");
		Long uid = Member.getUID();
		String userId = uid + "";
		Transaction transaction = new Transaction();
		if (StringUtils.isEmpty(id)) {
			// 新增公示
			EaoPublicity publicity = new EaoPublicity();
			publicity.setValue(this.Request);
			publicity.setId(UUID.randomUUID().toString());
			publicity.setState("0");// 状态(0草稿 1未审核2已发布)
			publicity.setPublicityTime(new Date());
			publicity.setIsDelete("0");
			publicity.setOperator(userId);// 当前登录人
			publicity.setVisitNumber(0);// 访问量
			String address = "";
			if (StringUtil.isNotEmpty(publicity.getMProvince())) {
				address = publicity.getMProvince();
			}
			if (StringUtil.isNotEmpty(publicity.getMCity())) {
				address = address + publicity.getMCity();
			}
			if (StringUtil.isNotEmpty(publicity.getMCounty())) {
				address = address + publicity.getMCounty();
			}
			if (StringUtil.isNotEmpty(publicity.getMAddress())) {
				address = address + publicity.getMAddress();
			}
			if (StringUtil.isNotEmpty(address)) {
				Map<String, Object> map = GetLatAndLng.getGoogleLatLng(address);
				publicity.setLng(map.get("lng").toString());
				publicity.setLat(map.get("lat").toString());
			}

			// 敏感词
			try {
				BanWordsUtil.initBanWordsList();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (StringUtil.isNotEmpty(publicity.getContent())) {
				List<String> banWordList = BanWordsUtil.searchBanWords(publicity.getContent());
				if (!banWordList.isEmpty()) {
					this.Response.put("_STATE", 2);
					this.Response.put("mgc", banWordList);
					f("在文本中发现敏感词，请修改");
				} else {
					transaction.insert(publicity);
					String enclosureList = $V("Enclosure");// 附件路径(每个附件路径间用“,”分隔)
					if (StringUtil.isNotEmpty(enclosureList)) {
						String[] strList = enclosureList.split(",");
						for (String string : strList) {
							EaoPublicityEnclosure enclosure = new EaoPublicityEnclosure();
							enclosure.setId(UUID.randomUUID().toString());
							enclosure.setPublicityId(publicity.getId());
							enclosure.setEnclosure(string);
							transaction.update(enclosure);
						}
					}
					if (transaction.commit()) {
						s("公示草稿保存成功");
					} else {
						f("公示草稿保存失败");
					}
				}
			}
		} else {
			// 更新公示
			DataTable dt = new Q("select * from eao_publicity where Id = ? ", id).fetch();
			if (dt.getRowCount() > 0) {
				EaoPublicity publicity = new EaoPublicity();
				publicity.setValue(dt.get(0));
				publicity.setState("0");
				publicity.setValue(this.Request);
				publicity.setIsDelete("0");
				String address = "";
				if (StringUtil.isNotEmpty(publicity.getMProvince())) {
					address = publicity.getMProvince();
				}
				if (StringUtil.isNotEmpty(publicity.getMCity())) {
					address = address + publicity.getMCity();
				}
				if (StringUtil.isNotEmpty(publicity.getMCounty())) {
					address = address + publicity.getMCounty();
				}
				if (StringUtil.isNotEmpty(publicity.getMAddress())) {
					address = address + publicity.getMAddress();
				}
				if (StringUtil.isNotEmpty(address)) {
					Map<String, Object> map = GetLatAndLng.getGoogleLatLng(address);
					publicity.setLng(map.get("lng").toString());
					publicity.setLat(map.get("lat").toString());
				}
				// 敏感词
				try {
					BanWordsUtil.initBanWordsList();
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (StringUtil.isNotEmpty(publicity.getContent())) {
					List<String> banWordList = BanWordsUtil.searchBanWords(publicity.getContent());
					if (!banWordList.isEmpty()) {
						for (String s : banWordList) {
							System.out.println("找到敏感词：" + s);
						}
						this.Response.put("_STATE", 2);
						this.Response.put("mgc", banWordList);
						f("在文本中发现敏感词，请修改");
					} else {
						transaction.update(publicity);
						// 附件更新把原附件删除重新添加
						DataTable table = new Q("select * from eao_publicity_enclosure where PublicityId = ? ", id)
								.fetch();
						if (table.getRowCount() > 0) {
							for (DataRow dataRow : table) {
								EaoPublicityEnclosure enclosure = new EaoPublicityEnclosure();
								enclosure.setValue(dataRow);
								transaction.delete(enclosure);
							}
						}
						String enclosureList = $V("Enclosure");// 附件路径(每个附件路径间用“,”分隔)
						if (StringUtil.isNotEmpty(enclosureList)) {
							String[] strList = enclosureList.split(",");
							for (String string : strList) {
								EaoPublicityEnclosure enclosure = new EaoPublicityEnclosure();
								enclosure.setId(UUID.randomUUID().toString());
								enclosure.setPublicityId(publicity.getId());
								enclosure.setEnclosure(string);
								transaction.update(enclosure);
							}
						}
						if (transaction.commit()) {
							s("公示草稿修改成功");
						} else {
							f("公示草稿修改失败");
						}
					}
				} else {
					f("更新的公示不存在");
				}

			}
		}
	}

	/**
	 * 查询当前登录人下是否有保存的草稿并展示
	 */
	@Priv(loginType = LoginType.Member)
	public void findThisLogin() {
		Long id = Member.getUID();
		DataTable dt = new Q("select * from eao_publicity where State = 0 and Operator = ?", id).fetch();
		List<EaoPublicity> list = new ArrayList<EaoPublicity>();
		if (dt.getRowCount() > 0) {
			for (DataRow dataRow : dt) {
				EaoPublicity publicity = new EaoPublicity();
				publicity.setValue(dataRow);
				list.add(publicity);
			}
			this.Response.put("publicCity", list);
		}
	}

	/**
	 * 发布公示
	 * 
	 * @throws Exception
	 * 
	 */
	@Priv(loginType = LoginType.Member)
	public void ReleasePublicity() throws Exception {
		String Id = $V("Id");
		Transaction transaction = new Transaction();
		DataTable dataTable = new Q("select * from eao_publicity_audit").fetch();
		EaoPublicityAudit publicityAudit = new EaoPublicityAudit();
		publicityAudit.setValue(dataTable.get(0));

		if (StringUtils.isEmpty(Id)) {
			// 新增公示
			EaoPublicity publicity = new EaoPublicity();
			publicity.setValue(this.Request);
			publicity.setId(UUID.randomUUID().toString());
			publicity.setIsDelete("0");
			publicity.setPublicityTime(new Date());
			publicity.setOperator(Long.toString(Member.getUID()));// 当前登录人
			publicity.setVisitNumber(0);// 访问量
			String address = "";
			if (StringUtil.isNotEmpty(publicity.getMProvince())) {
				address = publicity.getMProvince();
			}
			if (StringUtil.isNotEmpty(publicity.getMCity())) {
				address = address + publicity.getMCity();
			}
			if (StringUtil.isNotEmpty(publicity.getMCounty())) {
				address = address + publicity.getMCounty();
			}
			if (StringUtil.isNotEmpty(publicity.getMAddress())) {
				address = address + publicity.getMAddress();
			}
			if (StringUtil.isNotEmpty(address)) {
				Map<String, Object> map = GetLatAndLng.getGoogleLatLng(address);
				publicity.setLng(map.get("lng").toString());
				publicity.setLat(map.get("lat").toString());
			}
			;
			// 敏感词
			try {
				BanWordsUtil.initBanWordsList();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (StringUtil.isNotEmpty(publicity.getContent())) {
				List<String> banWordList = BanWordsUtil.searchBanWords(publicity.getContent());
				if (!banWordList.isEmpty()) {
					for (String s : banWordList) {
						System.out.println("找到敏感词：" + s);
					}
					this.Response.put("_STATE", 2);
					this.Response.put("mgc", banWordList);
					f("在文本中发现敏感词，请修改");
				} else {
					if ("0".equals(publicityAudit.getIsAudit())) {
						publicity.setState("2");// 状态(0草稿 1未审核2已发布)
						transaction.insert(publicity);
						String enclosureList = $V("Enclosure");// 附件路径(每个附件路径间用“,”分隔)
						if (StringUtil.isNotEmpty(enclosureList)) {
							String[] strList = enclosureList.split(",");
							for (String string : strList) {
								EaoPublicityEnclosure enclosure = new EaoPublicityEnclosure();
								enclosure.setId(UUID.randomUUID().toString());
								enclosure.setPublicityId(publicity.getId());
								enclosure.setEnclosure(string);
								transaction.update(enclosure);
							}
						}
						if (transaction.commit()) {
							s("公示发布成功");
						} else {
							f("公示发布失败");
						}
					} else {
						publicity.setState("1");// 状态(0草稿 1未审核2已发布)
						transaction.insert(publicity);
						String enclosureList = $V("Enclosure");// 附件路径(每个附件路径间用“,”分隔)
						if (StringUtil.isNotEmpty(enclosureList)) {
							String[] strList = enclosureList.split(",");
							for (String string : strList) {
								EaoPublicityEnclosure enclosure = new EaoPublicityEnclosure();
								enclosure.setId(UUID.randomUUID().toString());
								enclosure.setPublicityId(publicity.getId());
								enclosure.setEnclosure(string);
								transaction.update(enclosure);
							}
						}
						if (transaction.commit()) {
							s("公示发布请求已提交,等待审核");
						} else {
							f("公示发布请求提交失败");
						}
					}
				}
			}
		} else {
			// 更新公示
			DataTable dt = new Q("select * from eao_publicity where Id = ? ", Id).fetch();
			if (dt.getRowCount() > 0) {
				EaoPublicity publicity = new EaoPublicity();
				publicity.setValue(dt.get(0));
				publicity.setValue(this.Request);
				publicity.setVisitNumber(0);// 访问量
				publicity.setIsDelete("0");
				String address = "";
				if (StringUtil.isNotEmpty(publicity.getMProvince())) {
					address = publicity.getMProvince();
				}
				if (StringUtil.isNotEmpty(publicity.getMCity())) {
					address = address + publicity.getMCity();
				}
				if (StringUtil.isNotEmpty(publicity.getMCounty())) {
					address = address + publicity.getMCounty();
				}
				if (StringUtil.isNotEmpty(publicity.getMAddress())) {
					address = address + publicity.getMAddress();
				}
				if (StringUtil.isNotEmpty(address)) {
					Map<String, Object> map = GetLatAndLng.getGoogleLatLng(address);
					publicity.setLng(map.get("lng").toString());
					publicity.setLat(map.get("lat").toString());
				}
				// 敏感词
				try {
					BanWordsUtil.initBanWordsList();
				} catch (IOException e) {
					e.printStackTrace();
				}
				List<String> banWordList = BanWordsUtil.searchBanWords(publicity.getContent());
				if (!banWordList.isEmpty()) {
					this.Response.put("_STATE", 2);
					this.Response.put("mgc", banWordList);
					f("在文本中发现敏感词，请修改");
				} else {
					if ("0".equals(publicityAudit.getIsAudit())) {
						publicity.setState("2");// 状态(0草稿 1未审核2已发布)
						transaction.update(publicity);
						if (transaction.commit()) {
							s("公示更新发布成功");
						} else {
							f("公示更新发布失败");
						}
					} else {
						publicity.setState("1");// 状态(0草稿 1未审核2已发布)
						transaction.update(publicity);
						if (transaction.commit()) {
							s("公示发布请求已提交,等待审核");
						} else {
							f("公示发布请求提交失败");
						}
					}
					// 附件更新把原附件删除重新添加
					String enclosureList = $V("enclosure");// 附件路径(每个附件路径间用“,”分隔)
					DataTable table = new Q("select * from eao_publicity_enclosure where PublicityId = ? ", Id)
							.fetch();
					if (table.getRowCount() > 0) {
						for (DataRow dataRow : table) {
							EaoPublicityEnclosure enclosure = new EaoPublicityEnclosure();
							enclosure.setValue(dataRow);
							transaction.delete(enclosure);
							transaction.commit();
						}
					}
					if (StringUtil.isNotEmpty(enclosureList)) {
						String[] strList = enclosureList.split(",");
						for (String string : strList) {
							EaoPublicityEnclosure enclosure = new EaoPublicityEnclosure();
							enclosure.setId(UUID.randomUUID().toString());
							enclosure.setPublicityId(publicity.getId());
							enclosure.setEnclosure(string);
							transaction.update(enclosure);
						}
					}
				}
			} else {
				f("更新的公示不存在");
			}
		}
	}

	/**
	 * 登录人查询所有本人发布成功公示
	 */
	@Priv(loginType = LoginType.Member)
	public void findReleasePublicity() {
		int pageSize = this.Request.getInt("PageSize");
		if (pageSize == 0) {
			pageSize = 10;
		}
		int pageIndex = this.Request.getInt("PageIndex");
		String project_type = $V("ProjectType");
		Q q = new Q("select * from eao_publicity where State = 2 and IsDelete = 0 and Operator = ? ",
				UserInfo.getRealName());
		if (!StringUtils.isEmpty(project_type)) {
			q.append("and ProjectType = ?", project_type);
		}
		DataTable dt = q.fetch(pageSize, pageIndex);
		List<EaoPublicity> list = new ArrayList<>();
		for (DataRow dataRow : dt) {
			EaoPublicity publicity = new EaoPublicity();
			publicity.setValue(dataRow);
			list.add(publicity);
		}
		this.Response.put("publicity", list);
		Q q1 = new Q("select * from eao_publicity where State = 2 and IsDelete = 1 and Operator = ? ",
				UserInfo.getRealName());
		if (StringUtil.isNotEmpty(project_type)) {
			q1.append("and ProjectType = ?", project_type);
		}
		DataTable dataTable = q1.fetch(pageSize, pageIndex);
		List<EaoPublicity> lists = new ArrayList<>();
		for (DataRow dataRow : dataTable) {
			EaoPublicity publicity = new EaoPublicity();
			publicity.setValue(dataRow);
			lists.add(publicity);
		}
		this.Response.put("total", list.size() + lists.size());
		this.Response.put("publicityDelete", lists);
	}

	/**
	 * 模糊查询查询所有未审核公示
	 */
	@Priv(loginType = LoginType.Member)
	public void findAuditPublicity() {
		String title = $V("Title");
		int pageSize = this.Request.getInt("PageSize");
		if (pageSize == 0) {
			pageSize = 10;
		}
		int pageIndex = this.Request.getInt("PageIndex");
		Q noQ = new Q(
				"select ep.*,em.RealName from eao_publicity ep LEFT JOIN Ec_Member em on ep.Operator=em.Id where ep.State = 1 and ep.IsDelete = 0");
		if (StringUtil.isNotEmpty(title)) {
			noQ.append(" and ep.Title like ?", "%" + title + "%");
		}
		DataTable noDtNum = new Q("select count(1) as num from eao_publicity where State = 1 and IsDelete = 0").fetch();
		if (StringUtil.isNotEmpty(title)) {
			noQ.append(" and ep.Title like ?", "%" + title + "%");
		}
		DataTable noDt = noQ.fetch(pageSize, pageIndex);
		List<EaoPublicityVo> noPublicCityList = new ArrayList<EaoPublicityVo>();
		if (noDt.getRowCount() > 0) {
			for (DataRow dataRow : noDt) {
				EaoPublicityVo publicity = new EaoPublicityVo();
				publicity.setId(dataRow.getString("Id"));
				publicity.setTitle(dataRow.getString("Title"));
				publicity.setRealName(dataRow.getString("RealName"));
				publicity.setCreateTime(dataRow.getString("PublicityTime"));
				noPublicCityList.add(publicity);
			}
			this.Response.put("publicity", noPublicCityList);
		} else {
			$F("模糊查询未审核公示失败");
		}
		Integer noPubliCityNum = 0;
		if (noDtNum.getRowCount() > 0) {
			for (DataRow dataRow : noDtNum) {
				noPubliCityNum = dataRow.getInt("num");
			}
		}
		this.Response.put("total", noPubliCityNum);
	}

	/**
	 * 根据id查询公示
	 */
	@Priv(login = false)
	public void findByPublicityId() {
		String Id = $V("Id");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Transaction transaction = new Transaction();
		DataTable dt = new Q("select * from eao_publicity where Id = ?", Id).fetch();
		EaoPublicity publicity = new EaoPublicity();
		if (dt.getRowCount() != 0) {
			publicity.setValue(dt.get(0));
			Integer i = publicity.getVisitNumber();
			publicity.setVisitNumber(i + 1);
			transaction.update(publicity);
		}
		DataTable dataTable = new Q("select * from eao_publicity_enclosure where PublicityId = ?",
				publicity.getFileID()).fetch();
		List<EaoPublicityEnclosure> list = new ArrayList<EaoPublicityEnclosure>();
		for (DataRow dataRow : dataTable) {
			EaoPublicityEnclosure publicityEnclosure = new EaoPublicityEnclosure();
			publicityEnclosure.setValue(dataRow);
			list.add(publicityEnclosure);
		}
		// 日访问量
		try {
			Date date = new Date();
			String str = sf.format(date);
			Date visit_time = sf.parse(str);
			DataTable table = new Q("select * from eao_publicity_visit where VisitTime = ?", visit_time).fetch();
			EaoPublicityVisit publicityVisit = new EaoPublicityVisit();
			if (table.getRowCount() > 0) {
				publicityVisit.setValue(table.get(0));
				Integer Visit = publicityVisit.getVisitNumber();
				publicityVisit.setVisitNumber(Visit + 1);
				transaction.update(publicityVisit);
			} else {
				publicityVisit.setId(UUID.randomUUID().toString());
				publicityVisit.setVisitTime(visit_time);
				publicityVisit.setVisitNumber(1);
				transaction.insert(publicityVisit);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		transaction.commit();
		this.Response.put("publicity", publicity);
		this.Response.put("PublicityEnclosure", list);
	}

	/**
	 * 根据id查询公示附件
	 */
	@Priv(login = false)
	public void findByEnclosure() {
		String Id = $V("Id");
		String fileId = $V("FileId");
		if (StringUtil.isNotEmpty(Id)) {
			DataTable dt = new Q("select * from eao_publicity where Id = ?", Id).fetch();
			EaoPublicity publicity = new EaoPublicity();
			if (dt.getRowCount() != 0) {
				publicity.setValue(dt.get(0));
			}
			if (StringUtils.isEmpty(fileId)) {
				fileId = publicity.getFileID();
			}
		}
		DataTable dataTable = new Q("select * from eao_publicity_enclosure where PublicityId = ? ", fileId).fetch();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (dataTable.getRowCount() != 0) {
			for (DataRow dataRow : dataTable) {
				Map<String, Object> map = new HashMap<>();
				EaoPublicityEnclosure publicityEnclosure = new EaoPublicityEnclosure();
				publicityEnclosure.setValue(dataRow);
				map.put("Id", publicityEnclosure.getId());
				map.put("PublicityId", publicityEnclosure.getPublicityId());
				map.put("Enclosure", publicityEnclosure.getEnclosure());
				String[] str = publicityEnclosure.getEnclosure().split("/");
				String strName = str[str.length - 1];
				String[] stringName = strName.split("\\?");
				String name = stringName[0].substring(5);
				map.put("Name", name);
				list.add(map);
			}
		}
		this.Response.put("PublicityEnclosure", list);
	}

	/**
	 * 审核公示
	 */
	@Priv(loginType = LoginType.Member)
	public void AuditPublicity() {
		String Id = $V("Id");
		String State = $V("State");
		DataTable dt = new Q("select * from eao_publicity where id = ?", Id).fetch();
		if (dt.getRowCount() != 0) {
			Transaction transaction = new Transaction();
			EaoPublicity publicity = new EaoPublicity();
			publicity.setValue(dt.get(0));
			publicity.setState(State);
			transaction.update(publicity);
			if (transaction.commit()) {
				if ("2".equals(State)) {
					s("公示审核通过");
				} else if ("0".equals(State)) {
					f("公示审核未通过");
				}
			} else {
				f("公示审核失败");
			}
		}
	}

	/**
	 * 根据id删除多条公示
	 */
	@Priv(loginType = LoginType.Member)
	public void deletePublicityId() {
		Long userId = Member.getUID();
		String str = $V("Id");
		Transaction transaction = new Transaction();
		DataTable dtCG = new Q("select Type from Ec_Member where id = ?", userId).fetch();
		DataTable dtE = new Q("select * from Mh_Resource where Member = ?", userId).fetch();
		if (dtCG.getRowCount() != 0) {
			if (dtCG.get(0).get("Type").equals("CG")) {
				String[] idList = str.split(",");
				for (String id : idList) {
					DataTable dt = new Q("select * from eao_publicity where Id = ?", id).fetch();
					EaoPublicity publicity = new EaoPublicity();
					publicity.setValue(dt.get(0));
					publicity.setIsDelete("1");
					publicity.setState("0");
					transaction.update(publicity);
				}
				if (transaction.commit()) {
					s("系统管理员公示删除成功");
				} else {
					f("系统管理员公示删除失败");
				}
			} else if (dtCG.get(0).get("Type").equals("Ent") || dtE.getRowCount() != 0) {
				if (StringUtil.isNotEmpty(str)) {
					String[] idList = str.split(",");
					for (String id : idList) {
						Q q2 = new Q(" delete from eao_publicity where ").in("Id", id);
						transaction.add(q2);
					}
					if (transaction.commit()) {
						s("普通管理员公示删除成功");
					} else {
						f("普通管理员公示删除失败");
					}
				}
			}
		}
	}

	/**
	 * 根据id删除未审核的多条公示
	 */
	@Priv(loginType = LoginType.Member)
	public void deleteNoPublicityId() {
		String str = $V("Id");
		if (StringUtil.isNotEmpty(str)) {
			String[] idList = str.split(",");
			Transaction transaction = new Transaction();
			for (String id : idList) {
				DataTable dt = new Q("select * from eao_publicity where state = 1 and Id = ?", id).fetch();
				if (dt.getRowCount() != 0) {
					EaoPublicity publicity = new EaoPublicity();
					publicity.setValue(dt.get(0));
					publicity.setIsDelete("1");
					transaction.update(publicity);
				}
			}
			if (transaction.commit()) {
				s("公示删除成功");
			} else {
				f("公示删除失败");
			}
		}
	}

	/**
	 * 查询公示(模糊查询：省,城市，报告类型，标题，时间段)，地图显示
	 */
	@Priv(login = false)
	public void findByPublicity() {
		String MProvince = $V("MProvince");// 省份
		String MCity = $V("MCity");// 城市
		String ReportType = $V("ReportType");// 报告类型(报告表，报告书...)
		String Title = $V("Title");// 标题
		String StartTime = $V("StartTime");// 起始时间
		String EndTime = $V("EndTime");// 结束时间
		String ProjectType = $V("ProjectType");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		// 查询sql的拼接
		Q q = new Q("select * from eao_publicity where 1 = 1 and State = 2 and IsDelete = 0");
		if (StringUtil.isNotEmpty(MProvince)) {
			q.append(" and MProvince = ? ", MProvince);
		}
		if (StringUtil.isNotEmpty(MCity)) {
			q.append(" and MCity = ? ", MCity);
		}
		if (StringUtil.isNotEmpty(ReportType)) {
			q.append(" and ReportType = ? ", ReportType);
		}
		if (StringUtil.isNotEmpty(Title)) {
			q.append(" and Title like ?", "%" + Title + "%");
		}
		if (StringUtil.isNotEmpty(ProjectType)) {
			q.and().eq("ProjectType", ProjectType);
		}
		try {
			if (StringUtil.isNotEmpty(StartTime) && StringUtil.isNotEmpty(EndTime)) {
				Date startdate = sf.parse(StartTime);
				Date enddate = sf.parse(EndTime);
				q.append(" and DATE_FORMAT(PublicityTime,'%Y-%m-%d') between ? and ?", startdate, enddate);
			} else if (StringUtil.isNotEmpty(StartTime) && !StringUtil.isNotEmpty(EndTime)) {
				Date startdate = sf.parse(StartTime);
				EndTime = new Date().toString();
				Date enddate = sf.parse(EndTime);
				q.append(" and DATE_FORMAT(PublicityTime,'%Y-%m-%d') between ? and ?", startdate, enddate);
			} else if (!StringUtil.isNotEmpty(StartTime) && StringUtil.isNotEmpty(EndTime)) {
				Date enddate = sf.parse(EndTime);
				q.append(" and DATE_FORMAT(PublicityTime,'%Y-%m-%d') <? ", enddate);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		DataTable dataTable = q.fetch();
		List<EaoPublicity> list = new ArrayList<EaoPublicity>();
		for (DataRow dataRow : dataTable) {
			EaoPublicity publicity = new EaoPublicity();
			publicity.setValue(dataRow);
			list.add(publicity);
		}
		this.Response.put("publicity", list);

	}

	/**
	 * 后台公示统计
	 * 
	 * @throws ParseException
	 */
	@Priv(loginType = LoginType.Member)
	public void visit() throws ParseException {
		String account = $V("Account");
		Set<Integer> accountId = new HashSet<Integer>();
		if (StringUtil.isNotEmpty(account)) {
			DataTable dt = new Q(
					"select mr.member as Id from Crm_Company  as cc ,Mh_Resource as mr where cc.Id = mr.Company and cc.Id = ?",
					account).fetch();
			if (dt.getRowCount() != 0) {
				for (int i = 0; i < dt.getRowCount(); i++) {
					accountId.add(dt.get(i).getInt("Id"));
				}
			}
		}

		String MProvince = $V("MProvince");// 省份
		String MCity = $V("MCity");// 城市
		String ReportType = $V("ReportType");// 项目类型(一次公示，二次公示...)
		String startTime = $V("StartTime");// 起始时间
		String endTime = $V("EndTime");// 结束时间
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		Date dBegin = null;
		Date dEnd = null;
		List<String> strList = new ArrayList<>();
		FindDateUtil findDateUtil = new FindDateUtil();
		Q q = new Q("select count(Id),SUM(VisitNumber) from eao_publicity where State = '2' and IsDelete = 0");
		Q qt = new Q("select Id , ProjectType from eao_publicity where State = '2' and IsDelete = 0");
		Q qz = new Q(
				"select DATE_FORMAT(PublicityTime,'%Y-%m-%d') , count(PublicityTime) from eao_publicity where State = '2' and IsDelete = 0");
		if (StringUtil.isNotEmpty(MProvince)) {
			q.and().eq("MProvince", MProvince);
			qt.and().eq("MProvince", MProvince);
			qz.and().eq("MProvince", MProvince);
		}
		if (StringUtil.isNotEmpty(MCity)) {
			q.and().eq("MCity", MCity);
			qt.and().eq("MCity", MCity);
			qz.and().eq("MCity", MCity);
		}
		if (StringUtil.isNotEmpty(ReportType)) {
			q.and().eq("ProjectType", ReportType);
			qt.and().eq("ProjectType", ReportType);
			qz.and().eq("ProjectType", ReportType);
		}
		if (StringUtil.isNotEmpty(account)) {
			q.and().in("Operator", accountId);
			qt.and().in("Operator", accountId);
			qz.and().in("Operator", accountId);
		}
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			dEnd = sd.parse(endTime);
			dBegin = sd.parse(startTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			String sql = " and DATE_FORMAT(PublicityTime,'%Y-%m-%d') between ? and ?";
			q.append(sql, startTime, endTime);
			qt.append(sql, startTime, endTime);
			qz.append(sql, startTime, endTime);
		} else if (StringUtil.isNotEmpty(startTime) && !StringUtil.isNotEmpty(endTime)) {
			Date dtEnd = new Date();
			endTime = sd.format(dtEnd);
			dEnd = sd.parse(endTime);
			dBegin = sd.parse(startTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			String sql = " and DATE_FORMAT(PublicityTime,'%Y-%m-%d') between ? and ?";
			q.append(sql, startTime, endTime);
			qt.append(sql, startTime, endTime);
			qz.append(sql, startTime, endTime);
		} else if (!StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			dEnd = sd.parse(endTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			String sql = " and DATE_FORMAT(PublicityTime,'%Y-%m-%d') <DATE_SUB( ? , INTERVAL 1 MONTH) ";
			q.append(sql, endTime);
			qt.append(sql, endTime);
			qz.append(sql, endTime);
		} else if (!StringUtil.isNotEmpty(startTime) && !StringUtil.isNotEmpty(endTime)) {
			Date dtEnd = new Date();
			endTime = sd.format(dtEnd);
			dEnd = sd.parse(endTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			qz.append(" and DATE_FORMAT(PublicityTime,'%Y-%m-%d') > DATE_SUB( CURDATE() , INTERVAL 1 MONTH) ");
		}

		qz.append(" GROUP BY DATE_FORMAT(PublicityTime,'%Y-%m-%d')");
		Integer counteq = 0;
		if ("一次公示".equals(ReportType)) {
			this.Response.put("counteq", counteq);
		} else {
			DataTable dataTable = qt.fetch();
			for (DataRow dataRow : dataTable) {
				if (!"一次公示".equals(dataRow.get(1))) {
					DataTable table = new Q("select * from eao_publicity_enclosure where PublicityId = ?",
							dataRow.get(0).toString()).fetch();
					if (table.getRowCount() > 0) {
						counteq++;
					}
				}
			}
		}
		DataTable dt = q.fetch();
		String countId = dt.get(0, 0).toString();
		String SUMvisit = "";
		if (dt.get(0, 1) == null) {
			SUMvisit = "0";
		} else {
			SUMvisit = dt.get(0, 1).toString();
		}
		this.Response.put("countId", countId);
		this.Response.put("SUMvisit", SUMvisit);
		this.Response.put("counteq", counteq);

		// 后台公示数量统计(折线图)
		DataTable dataTable = qz.fetch();
		List<Map<String, String>> list = new ArrayList<>();
		List<String> strs = new ArrayList<>();
		Map<String, String> map1 = new HashMap<>();
		for (DataRow dataRow : dataTable) {
			for (String str : strList) {
				String AddTime = null;
				String countAddTime = "0";
				AddTime = dataRow.get(0).toString();
				countAddTime = dataRow.get(1).toString();
				if (str.equals(AddTime)) {
					map1.put("date", countAddTime);
					map1.put(AddTime, countAddTime);
					strs.add(AddTime);
				}
			}
		}

		for (String str : strList) {
			Map<String, String> map = new HashMap<>();
			Boolean lean = true;
			String countAddTime = "0";
			for (String map2 : strs) {
				if (str.equals(map2)) {
					lean = false;
				}
			}
			if (lean) {
				map.put("date", str);
				map.put("count", countAddTime);
				list.add(map);
			} else {
				map.put("date", str);
				map.put("count", map1.get(str));
				list.add(map);
			}
		}

		this.Response.put("publicityTime", list);

	}

	/**
	 * 后台公示日访问量统计(折线图)
	 * 
	 * @throws ParseException
	 */
	@Priv(loginType = LoginType.Member)
	public void publicityVisit() throws ParseException {
		String startTime = $V("StartTime");// 起始时间
		String endTime = $V("EndTime");// 结束时间
		List<String> strList = new ArrayList<>();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		Date dBegin = null;
		Date dEnd = null;
		FindDateUtil findDateUtil = new FindDateUtil();
		Q q = new Q("select DATE_FORMAT(VisitTime,'%Y-%m-%d') , VisitNumber from eao_publicity_visit where 1 = 1 ");
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			dEnd = sd.parse(endTime);
			dBegin = sd.parse(startTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			q.append(" and DATE_FORMAT(VisitTime,'%Y-%m-%d') between ? and ?", startTime, endTime);
		} else if (StringUtil.isNotEmpty(startTime) && !StringUtil.isNotEmpty(endTime)) {
			Date dtEnd = new Date();
			endTime = sd.format(dtEnd);
			dEnd = sd.parse(endTime);
			dBegin = sd.parse(startTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			q.append(" and DATE_FORMAT(VisitTime,'%Y-%m-%d') between ? and ?", startTime, endTime);
		} else if (!StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			dEnd = sd.parse(endTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			q.append(" and DATE_FORMAT(VisitTime,'%Y-%m-%d') <DATE_SUB( ? , INTERVAL 1 MONTH) ", endTime);
		} else if (!StringUtil.isNotEmpty(startTime) && !StringUtil.isNotEmpty(endTime)) {
			Date dtEnd = new Date();
			endTime = sd.format(dtEnd);
			dEnd = sd.parse(endTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			q.append(" and DATE_FORMAT(VisiTime,'%Y-%m-%d') > DATE_SUB( CURDATE() , INTERVAL 1 MONTH) ");
		}
		q.orderby("VisitTime desc");
		DataTable dt = q.fetch();
		List<Map<String, String>> list = new ArrayList<>();
		List<String> strs = new ArrayList<>();
		Map<String, String> map1 = new HashMap<>();
		for (DataRow dataRow : dt) {
			for (String str : strList) {
				String visit_time = null;
				String visit_number = "0";
				visit_time = dataRow.get(0).toString();
				visit_number = dataRow.get(1).toString();
				if (str.equals(visit_time)) {
					map1.put("date", visit_number);
					map1.put(visit_time, visit_number);
					strs.add(visit_time);
				}
			}
		}

		for (String str : strList) {
			Map<String, String> map = new HashMap<>();
			Boolean lean = true;
			String countAddTime = "0";
			for (String map2 : strs) {
				if (str.equals(map2)) {
					lean = false;
				}
			}
			if (lean) {
				map.put("date", str);
				map.put("count", countAddTime);
				list.add(map);
			} else {
				map.put("date", str);
				map.put("count", map1.get(str));
				list.add(map);
			}
		}

		this.Response.put("publicityTime", list);

	}

	/**
	 * 根据登录人id查询本人所发布公示的次数
	 */
	@Priv(loginType = LoginType.Member)
	public void publiCityNum() {
		Long id = Member.getUID();
		int num = 0;
		DataTable dt = new Q("select count(1) as num from eao_publicity where Operator = ? ", id).fetch();
		if (dt.getRowCount() != 0) {
			for (DataRow dataRow : dt) {
				num = dataRow.getInt("num");
			}
			this.Response.put("publiCityNum", num);
		} else {
			$F("查询失败");
		}

	}

	/**
	 * getHot 最热的已发布12条公示
	 */
	@Priv(login=false)
	public void getHot() {
		DataTable dt = new Q(
				"select * from eao_publicity where State = '2' and IsDelete = '0' order by VisitNumber desc,PublicityTime desc limit 0,12")
						.fetch();
		if (dt.getRowCount() != 0) {
			List<EaoPublicity> publicityList = new ArrayList<>();
			for (DataRow dataRow : dt) {
				EaoPublicity publicity = new EaoPublicity();
				publicity.setValue(dataRow);
				publicityList.add(publicity);
			}
			this.Response.put("publicityList", publicityList);
		} else {
			$F("查询失败");
		}

	}

	/**
	 * 公示范文
	 */
	@Priv(loginType = LoginType.Member)
	public void getPublicityType() {
		String PublicityType = $V("PublicityType");
		DataTable dt = new Q("select * from eao_publicity_type where Type = ? ", PublicityType).fetch();
		if (dt.getRowCount() != 0) {
			EaoPublicityTypeVo pt = new EaoPublicityTypeVo();
			pt.setId(dt.get(0).getString("Id"));
			pt.setContent(dt.get(0).getString("Content"));
			pt.setType(dt.get(0).getInt("Type"));
			pt.setCreateTime(dt.get(0).getDate("CreateTime"));

			this.Response.put("publicityType", pt);
		} else {
			$F("查询失败");
		}

	}

	/**
	 * 上一条下一条
	 * 
	 * @throws ParseException
	 */

	@Priv(login=false)
	public void getLastNext() throws ParseException {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Transaction transaction = new Transaction();
		String id = $V("Id");
		String isHot = $V("IsHot");
		if ("true".equals(isHot) || "false".equals(isHot)) {
			isHot = "";
		}
		Boolean isIndex = Boolean.valueOf($V("IsIndex"));
		DataTable d = new Q("select  * from eao_publicity where id = ?", id).fetch();
		if (d.getRowCount() != 0) {
			// 公示访问量先+1
			EaoPublicity publicity = new EaoPublicity();
			publicity.setValue(d.get(0));
			Integer i = publicity.getVisitNumber();
			publicity.setVisitNumber(i + 1);
			transaction.update(publicity);
			// 日访问量+1
			Date date = new Date();
			String str = sf.format(date);
			Date VisitTime = sf.parse(str);
			DataTable table = new Q("select * from eao_publicity_visit where VisitTime = ?", VisitTime).fetch();
			EaoPublicityVisit publicityVisit = new EaoPublicityVisit();
			if (table.getRowCount() > 0) {
				publicityVisit.setValue(table.get(0));
				Integer Visit = publicityVisit.getVisitNumber();
				publicityVisit.setVisitNumber(Visit + 1);
				transaction.update(publicityVisit);
			} else {
				publicityVisit.setId(UUID.randomUUID().toString());
				publicityVisit.setVisitTime(VisitTime);
				publicityVisit.setVisitNumber(1);
				transaction.insert(publicityVisit);
			}
			boolean isOne = false;
			EaoPublicity publicityTemp = new EaoPublicity();
			publicityTemp.setValue(d.get(0));
			DataTable visit_number = new Q("select  VisitNumber from eao_publicity where VisitNumber = ?",
					publicityTemp.getVisitNumber()).fetch();
			if (visit_number.getRowCount() == 1) {
				isOne = false;
			} else {
				isOne = true;
			}
			Q q = new Q("(select  * from eao_publicity where 1=1 and State = ? ", publicityTemp.getState());
			Q q2 = new Q("(select  * from eao_publicity where 1=1 and State = ? ", publicityTemp.getState());
			if (!"0".equals(publicityTemp.getState())) {
				q.and().eq("IsDelete", "0");
				q2.and().eq("IsDelete", "0");
			}
			if (StringUtil.isEmpty(isHot)) {
				this.Response.put("isHot", "");
				q.and().append(" PublicityTime > ?", publicityTemp.getPublicityTime());
				if (!isIndex) {
					q.and().eq("ProjectType", publicityTemp.getProjectType());
				}
				q.append(" order by publicityTime asc limit 0,1)");
			} else {
				this.Response.put("isHot", "isHot");
				if (isOne) {
					q2.and().append(" VisitNumber = ?", publicityTemp.getVisitNumber());
					q2.and().append(" PublicityTime > ?", publicityTemp.getPublicityTime());
					q2.append(" order by VisitNumber,PublicityTime asc limit 0,1)");
					DataTable dt = q2.fetch();
					DataTable dt2 = new Q("select * from eao_publicity where VisitNumber > ?",
							publicityTemp.getVisitNumber()).fetch();
					if (dt.getRowCount() == 0 && dt2.getRowCount() != 0) {
						q.and().append(" VisitNumber > ?", publicityTemp.getVisitNumber());
					} else {
						q.and().append(" VisitNumber = ?", publicityTemp.getVisitNumber());
						q.and().append(" PublicityTime > ?", publicityTemp.getPublicityTime());
					}
					q.append(" order by VisitNumber,PublicityTime asc limit 0,1)");
				} else {
					q.and().append(" VisitNumber > ?", publicityTemp.getVisitNumber());
					q.append(" order by VisitNumber,PublicityTime asc limit 0,1)");
				}

			}
			q.append(" UNION ALL");
			q.append(" (select  * from eao_publicity where Id = ?)", id);
			q.append(" UNION ALL");
			q.append(" (select  * from eao_publicity where 1=1 and State = ? ", publicityTemp.getState());
			Q q3 = new Q("select  * from eao_publicity where 1=1 and State = ? ", publicityTemp.getState());
			if (!"0".equals(publicityTemp.getState())) {
				q.and().eq("IsDelete", "0");
				q3.and().eq("IsDelete", "0");
			}
			if (StringUtil.isEmpty(isHot)) {
				this.Response.put("IsHot", "");
				q.and().append(" PublicityTime < ?", publicityTemp.getPublicityTime());
				if (!isIndex) {
					q.and().eq("ProjectType", publicityTemp.getProjectType());
				}
				q.append(" order by PublicityTime desc limit 0,1)");
			} else {
				this.Response.put("isHot", "isHot");
				if (isOne) {
					q3.and().append(" VisitNumber = ?", publicityTemp.getVisitNumber());
					q3.and().append(" PublicityTime < ?", publicityTemp.getPublicityTime());
					q3.append(" order by VisitNumber desc,PublicityTime desc limit 0,1");
					DataTable dt = q3.fetch();
					DataTable dt2 = new Q("select * from eao_publicity where VisitNumber < ?",
							publicityTemp.getVisitNumber()).fetch();
					if (dt.getRowCount() == 0 && dt2.getRowCount() != 0) {
						q.and().append(" VisitNumber < ?", publicityTemp.getVisitNumber());
					} else {
						q.and().append(" VisitNumber = ?", publicityTemp.getVisitNumber());
						q.and().append(" PublicityTime < ?", publicityTemp.getPublicityTime());
					}
					q.append(" order by VisitNumber desc,PublicityTime desc limit 0,1)");

				} else {
					q.and().append(" VisitNumber < ?", publicityTemp.getVisitNumber());
					q.append(" order by VisitNumber desc,PublicityTime desc limit 0,1)");
				}

			}
			DataTable dt = q.fetch();
			if (dt.getRowCount() != 0) {
				List<EaoPublicity> publicityList = new ArrayList<>();
				for (DataRow dataRow : dt) {
					EaoPublicity Temp = new EaoPublicity();
					Temp.setValue(dataRow);
					publicityList.add(Temp);
				}
				if (publicityList.size() == 3) {
					this.Response.put("last", publicityList.get(0));
					this.Response.put("this", publicityList.get(1));
					this.Response.put("next", publicityList.get(2));
				}
				if (publicityList.size() == 2) {
					if (id.equals(publicityList.get(0).getId())) {
						this.Response.put("last", "");
						this.Response.put("this", publicityList.get(0));
						this.Response.put("next", publicityList.get(1));
					} else {
						this.Response.put("last", publicityList.get(0));
						this.Response.put("this", publicityList.get(1));
						this.Response.put("next", "");
					}
				}
				if (publicityList.size() == 1) {
					this.Response.put("last", "");
					this.Response.put("this", publicityList.get(0));
					this.Response.put("next", "");
				}
			}
			this.Response.put("isIndex", isIndex);
			transaction.commit();
		} else {
			f("查询失败");
		}

	}

	/**
	 * 文件删除
	 */
	@Priv(loginType = LoginType.Member)
	public void delete() {
		String Id = $V("Id");
		DataTable dataTable = new Q(" select * from eao_publicity_enclosure where Id = ? ", Id).fetch();
		if (dataTable.getRowCount() != 0) {
			EaoPublicityEnclosure enclosure = new EaoPublicityEnclosure();
			enclosure.setValue(dataTable.get(0));
			if (enclosure.delete()) {
				s("文件删除成功");
			} else {
				f("文件删除失败");
			}
		} else {
			f("请选择你要删除的文件");
		}
	}
}
